<?php
require_once 'vendor/autoload.php';

$url = $argv[1];

$client = new \GuzzleHttp\Client([
    'proxy' => ['http'=>'http://127.0.0.1:10809','https' => 'http://127.0.0.1:10809'],
    'allow_redirects' => true,
    'headers' => [
        'referer' => $url ,
        'cookie' => '__cfduid=d3185d7f658f44fc71db5792c0535e45b1610629320; sl=dm_1; sk=8fm6t464n2y2rvhl2h8neom0158q; s=b4af49a60; event=1612937685; ipb_session_id=0f123dca8ce33e6e515b43916c5ec05f; ipb_member_id=5671591; ipb_pass_hash=e8d57f8be0fed9767c5bd627b50c218e',
    ],
   // 'debug' => true
]);

$html = $client->get($url)->getBody()->getContents();

$dom = \voku\helper\HtmlDomParser::str_get_html($html);

$title = $dom->findOne('#gn')->text();

$saveDir = "storage/$title/";
@mkdir($saveDir);

$pageCount = $dom->find('table.ptt td')->length - 2;

$inc = 0;

$hrefArr = [];
$imageArr = [];
//同步版
for ($i = 1; $i <= $pageCount; $i++) {

    if ($i > 1) {

        $html = $client->get($url.'?p='.($i -1) )->getBody()->getContents();

        $dom = \voku\helper\HtmlDomParser::str_get_html($html);

    }

    $ret = $dom->findMultiOrFalse('#gdt .gdtm a');
    if ($ret) {
        foreach ($ret as $a) {
            $hrefArr[] =  $a->getAttribute('href');
//            $d = \voku\helper\HtmlDomParser::str_get_html($client->get($href)->getBody()->getContents());
//            $src = $d->findOne('#i7 a')->getAttribute('href');
//            $resp = $client->get($src);
//            file_put_contents($saveDir . $inc++ .'.'.$resp->getHeaderLine('Content-Type'), $resp->getBody()->getContents());
        }
    }
}

$requests =function($hrefArr)use($client,$url){
    foreach ($hrefArr as $href) {
        yield new \GuzzleHttp\Psr7\Request('GET',$href,[
            'referer' => $href
        ]);
    }
};
$pool = new \GuzzleHttp\Pool($client,$requests($hrefArr),[
    'concurrency' => 20,
    'fulfilled' => function(\GuzzleHttp\Psr7\Response $response,$index)use(&$imageArr){

        $imageArr[] = htmlspecialchars_decode(\voku\helper\HtmlDomParser::str_get_html($response->getBody()->getContents())->findOne('#i7 a')->getAttribute('href'));
    },
    'rejected' => function(\GuzzleHttp\Exception\RequestException $reason, $index){
        $resp = $reason->getResponse();
        print_r($resp->getStatusCode());
//        print_r($resp->getHeaders());
        print_r($resp->getBody()->getContents());
    }
]);
$pool->promise()->wait();

print_r($imageArr);

$pool2 = new \GuzzleHttp\Pool($client, $requests($imageArr),[
    'concurrency' => 20,
    'fulfilled' => function(\GuzzleHttp\Psr7\Response $response,$index)use($saveDir){
        $type = $response->getHeaderLine('Content-Type');
        if (mb_strpos($type,'text/html') === false) {
            file_put_contents($saveDir . $index .".$type", $response->getBody()->getContents());
        }else{
            print_r($response->getBody()->getContents());
        }
    },
    'rejected' => function(\GuzzleHttp\Exception\RequestException $reason, $index){
        $resp = $reason->getResponse();
        print_r($resp->getStatusCode());
//        print_r($resp->getHeaders());
        print_r($resp->getBody()->getContents());
    }
]);

$pool2->promise()->wait();

